<?php
namespace app\common\service\finance;



use app\admin\model\user\User;
use app\admin\model\user\UserBank;
use app\admin\model\user\UserBill;
use app\admin\model\user\UserWithdraw;
use app\common\service\BaseService;
use think\facade\Db;

class Withdraw extends BaseService
{

    /**
     * Author:Yingbo Sun
     * 提现申请
     * @param $post
     * @return array
     */
    public static function cash($post)
    {
        //开启事务
        self::beginTrans();
        try {
            UserWithdraw::create($post);
            //扣除用户预扣余额
            $user = User::where('id',$post['uid'])->find();
            //使用php高精密函数 计算余额
            $money = bcsub($user->fake_money,$post['cash_price'],2);
            $where = ['id'=>$post['uid']];
            //给用户扣除虚拟余额
            $user->update(['fake_money'=>$money],$where);
            //提现记录
            UserBill::expend('余额提现',$post['uid'],$post['cash_price'],'now_money','withdraw');
            //提交事务
            self::commitTrans();
        }catch (\Exception $e){
            //回滚
            self::rollbackTrans();
            return self::fail($e->getMessage());
        }
        return self::fail('提现申请成功');
    }

    public static function list($page, $limit, $where)
    {
        $count = UserWithdraw::where($where)
            ->count();
        $list = UserWithdraw::with(['userWithdraw'])
            ->where($where)
            ->page($page, $limit)
            ->order('id','desc')
            ->select();
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list,
        ];
        return $data;
    }

}